package group2projman;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

// TODO: Check returned/added books against reservedQueue
// : TODO Do we wanna ensure ISBNs are non-negative?
class Book {			
	String title, author;
	
	/// @note Can be any number of digits. A value of zero is considered null
	int isbn;
	int loanPeriod;
	
	ArrayList<BookCopy> copies = new ArrayList<BookCopy>(); 
	
	Catalogue.Category category;
	
	private Queue<Member> reservedQueue = new LinkedList<Member>();
	
	Book(String pTitle, String pAuthor, int pIsbn,
			Catalogue.Category pCategory, int pReservationPeriod) {
		title = pTitle;
		author = pAuthor;
		isbn = pIsbn;
		loanPeriod = pReservationPeriod;
		category = pCategory;
	}
	
	public int getId() {
		return isbn;
	}
	
	/* TODO: removeCopy() that actually removes a copy?
	 * Likewise, removeTransaction() etc?
	 * If so, need to account for reference map 
	 */
	void removeCopy(BookCopy copy) {
		
	}
	
	Member popQueue() {
		return reservedQueue.remove();		
	}
	
	/**
	 * @return true if a reservation was made
	 */
	boolean addToQueue(Member member) {
		// Can any copies of the book be reserved?
		boolean reservable = false;
		
		if (loanPeriod == 0)
			return false;
		
		for (BookCopy copy : copies)
			if (copy.reservable()) {
				reservable = true;
				
				break;
			}
		
		if (!reservable)
			return false;
		
		// Yup. Add member to reservation queue
		reservedQueue.add(member);		
		
		return true;
	}
	
	/**
	 * @throws Exception if member is not in queue.
	 * TODO: Anyone know a better exception type? I guess we could derive one within the scope of Book.
	 */
	void removeFromQueue(Member member) throws Exception {
		boolean memberInQueue = reservedQueue.remove(member);
		
		if (!memberInQueue)
			throw new Exception("Member not in Book's reservation queue");
	}
	
	boolean queueEmpty() {
		return reservedQueue.isEmpty();
	}
	
	void clearQueue() {
		reservedQueue.clear();
	}
	
	
}
